package com.android.webframework.nav;

import java.util.ArrayList;
import com.android.webframework.R;
import com.android.webframework.RequestObject;
import com.android.webframework.ResponseObject;
import com.android.webframework.Step;
import com.android.webframework.WebFramework;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class StartActivity extends Activity {
	
	private EditText mSource;
	private EditText mDestination;
	private Button mButton;
	
	private String mSourceText;
	private String mDestinationText;
	
	private Intent mIntent;
	
	private final String GOOGLE_MAP_API = "http://maps.googleapis.com/maps/api/directions/json?sensor=false";

	private ResponseObject mResObj;
	private WebFramework mWebWrapper;
	
	private ArrayList<Step> mDirections;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.userinput);
		
		//Assign Views
		mSource = (EditText) findViewById(R.id.editText1);
		mDestination = (EditText) findViewById(R.id.editText2);
		mButton = (Button) findViewById(R.id.button1);
		
		//Register button listener
		mButton.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				
				//Show loading screen
				final ProgressDialog load = ProgressDialog.show(StartActivity.this, "", "Loading. Please wait...");//
				
				//Get Text from both editText fields
				mSourceText = mSource.getText().toString();
				mDestinationText = mDestination.getText().toString();
				
				//Check for empty Source or Destination
				if(!mSourceText.equals("") && !mDestinationText.equals("")) {
				
					//Create Intent with return and start DirectionsActivity
					mIntent = new Intent(StartActivity.this, DirectionsActivity.class);
					mIntent.putExtra("source", mSourceText);
					mIntent.putExtra("destination", mDestinationText);
				
					//Remove the spaces
					mSourceText = mSourceText.replaceAll(" ", "+");
					mDestinationText = mDestinationText.replaceAll(" ", "+");
				
					//Create RequestObject
					String api_url = GOOGLE_MAP_API + "&origin=" + mSourceText + "&destination=" + mDestinationText;
					try {
						mWebWrapper = new WebFramework();
						mWebWrapper.setRequestType("directions");
						mWebWrapper.addParameter("url", api_url);
						mWebWrapper.request();
						mResObj = mWebWrapper.response();
						mDirections = (ArrayList<Step>) mResObj.getResponse();
					} catch (Exception e) {
						System.out.println(e);
					}
					
					//Check for valid user Source and Destination input
					if(!mDirections.isEmpty()) {
						mIntent.putExtra("directions", mDirections);
						new Thread(new Runnable(){
							public void run() {
								// TODO Auto-generated method stub
								startActivity(mIntent);
								load.dismiss();//
							}							
						}).start();
					} else {
						load.dismiss();//
						Toast.makeText(StartActivity.this, "Invalid Source/Destination", Toast.LENGTH_SHORT).show();
					}
				} else {
					load.dismiss();//
					Toast.makeText(StartActivity.this, "Input a Source and Destination", Toast.LENGTH_SHORT).show();
				}
			}
			
		});
	}

}
